//! @Author: DengLibin 
//! @Date:  2022-04-22 13:36:21
//! @Description:
/// simple handle
use std::cell::Cell;

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

use actix_web::get;
use actix_web::{web::Data, HttpRequest, HttpResponse};
/// @Author: DengLibin 
/// @Date:  2022-04-22 14:00:45
/// @Description: 获取全局状态
#[get("/state/index")]
pub async fn index(
    counter_mutex: Data<Mutex<usize>>, //作为参数传过来
    counter_cell: Data<Cell<u32>>,
    counter_atomic: Data<AtomicUsize>,
    req: HttpRequest,
) -> HttpResponse {
    println!("{:?}", req);

    // Increment the counters
    *counter_mutex.lock().unwrap() += 1;
    counter_cell.set(counter_cell.get() + 1);
    counter_atomic.fetch_add(1, Ordering::SeqCst);

    let body = format!(
        "global mutex counter: {}, local counter: {}, global atomic counter: {}",
        *counter_mutex.lock().unwrap(),
        counter_cell.get(),
        counter_atomic.load(Ordering::SeqCst),
    );
    HttpResponse::Ok().body(body)
}
#[get("/state/index2")]
pub async fn index2(
    counter_mutex: Data<Mutex<usize>>,
    req: HttpRequest,
) -> HttpResponse {
    println!("{:?}", req);

    // Increment the counters
    *counter_mutex.lock().unwrap() += 1;
   

    let body = format!(
        "global mutex counter: {}",
        *counter_mutex.lock().unwrap()
    );
    HttpResponse::Ok().body(body)
}